package com.wen.controller;

import com.github.pagehelper.PageInfo;
import com.wen.pojo.Permission;
import com.wen.pojo.Role;
import com.wen.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 20212
 * 权限管理
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                                @RequestParam(name = "size", required = true, defaultValue = "1") Integer size) throws Exception {
        ModelAndView model = new ModelAndView();
        List<Role> all = roleService.findAll(page, size);
        PageInfo pageInfo = new PageInfo(all);
        model.addObject("pageInfo", pageInfo);
        model.setViewName("role-list");
        return model;
    }

    @RequestMapping("/add")
    public String add(Role role) {
        try {
            roleService.add(role);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/role/findAll";
    }

    @RequestMapping("/findById")
    public ModelAndView findById( Integer id) {
        ModelAndView model = new ModelAndView();
        Role byId = null;
        try {
            byId = roleService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addObject("role", byId);
        model.setViewName("role-show");
        return model ;

    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id) throws Exception {
        roleService.deleteById(id);
        return "redirect:/role/findAll";
    }

    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(Integer id) throws Exception {
        ModelAndView model = new ModelAndView();
        model.addObject("role", roleService.findById(id));
        model.setViewName("role-update");
        return model;
    }

    @RequestMapping("/updateById")
    public String toUpdate(Role role) throws Exception {
        roleService.updateById(role);
        return "redirect:/role/findAll";
    }

    @RequestMapping("/findRoleByIdAndAllPermission")
    public ModelAndView findRoleByIdAndAllPermission( @RequestParam("id") Integer roleId) throws Exception {
        ModelAndView model = new ModelAndView();

        Role role = roleService.findById(roleId);
        List<Permission> permissionList = roleService.findOtherPermissions(roleId);
        model.addObject("role", role);
        model.addObject("permissionList", permissionList);
        model.setViewName("role-permission-add");
        return model;
    }

    @RequestMapping("/addPermissionToRole")
    public String addPermissionToRole(Integer roleId, @RequestParam("ids") Integer[] permissionId) {
        roleService.addPermissionToRole(roleId, permissionId);
        return "redirect:/role/findAll";
    }
}
